package com.zoffy.oj_backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zoffy.oj_backend.annotation.AuthCheck;
import com.zoffy.oj_backend.common.BaseResponse;
import com.zoffy.oj_backend.common.IdRequest;
import com.zoffy.oj_backend.common.ResultUtils;
import com.zoffy.oj_backend.model.dto.question.QuestionAddRequest;
import com.zoffy.oj_backend.model.dto.question.QuestionQueryRequest;
import com.zoffy.oj_backend.model.dto.question.QuestionUpdateRequest;
import com.zoffy.oj_backend.model.enums.UserRole;
import com.zoffy.oj_backend.model.vo.QuestionTinyVo;
import com.zoffy.oj_backend.model.vo.QuestionVo;
import com.zoffy.oj_backend.service.QuestionService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/question")
public class QuestionController {
    @Resource
    private QuestionService questionService;
    @PostMapping(value = "/add")
    public BaseResponse<Long> addQuestion(HttpServletRequest request, @RequestBody QuestionAddRequest questionAddRequest){
        Long res = questionService.addQuestion(request, questionAddRequest);
        return ResultUtils.success(res);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteQuestion(@RequestBody IdRequest idRequest){
        Boolean res = questionService.deleteQuestion(idRequest);
        return ResultUtils.success(res);
    }
    @PostMapping("/update")
    @AuthCheck(mustRole = UserRole.USER)
    public BaseResponse<Boolean> updateQuestion(@RequestBody QuestionUpdateRequest questionUpdateRequest){
        Boolean res = questionService.updateQuestion(questionUpdateRequest);
        return ResultUtils.success(res);
    }
    @PostMapping(value = "/upload/judgeCases",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public BaseResponse<Boolean> uploadJudgeCases(HttpServletRequest request,@RequestPart MultipartFile file,long questionId){
        Boolean res = questionService.uploadJudgeCases(request, questionId, file);
        return ResultUtils.success(res);

    }
    @PostMapping("/list/page/vo")
    public BaseResponse<IPage<QuestionVo>> listQuestionVoByPage(@RequestBody QuestionQueryRequest questionQueryRequest){
        IPage<QuestionVo> questionVoIPage = questionService.listQuestionVoByPage(questionQueryRequest);
        return ResultUtils.success(questionVoIPage);
    }
    @PostMapping("/list/page/tiny/vo")
    public BaseResponse<IPage<QuestionTinyVo>> listQuestionTinyVoByPage(@RequestBody QuestionQueryRequest questionQueryRequest){
        IPage<QuestionTinyVo> questionTinyVoIPage = questionService.listQuestionTinyVoByPage(questionQueryRequest);
        return ResultUtils.success(questionTinyVoIPage);
    }
    @PostMapping("/my/list/page/vo")
    public BaseResponse<IPage<QuestionVo>> listMyQuestionVoByPage(HttpServletRequest request,@RequestBody QuestionQueryRequest questionQueryRequest){
        IPage<QuestionVo> questionVoIPage = questionService.listMyQuestionVoByPage(request,questionQueryRequest);
        return ResultUtils.success(questionVoIPage);
    }
    @GetMapping("/get")
    public BaseResponse<QuestionVo> getQuestionById(long id){
        QuestionVo questionVo = questionService.getQuestionVoById(id);
        return ResultUtils.success(questionVo);
    }
}
